#!/bin/sh
# PCP QA Test No. 503
# Test pmlogrewrite use with pmlogger_daily
#
# Copyright (c) 2011 Ken McDonell.  All Rights Reserved.
#
# check-group-include: pmlogextract
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

PMLOGREWRITE=`which pmlogrewrite 2>/dev/null`
[ -n "$PMLOGREWRITE" ] || _notrun "pmlogrewrite not installed"

NEEDREWRITE=$PCP_LOG_DIR/pmlogger/.NeedRewrite

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp.*
    [ -f $NEEDREWRITE.$seq ] && $sudo mv $NEEDREWRITE.$seq $NEEDREWRITE
}

status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

[ -f $NEEDREWRITE ] && $sudo mv $NEEDREWRITE $NEEDREWRITE.$seq

hostname=`hostname | sed -e 's/\..*//'`

_filter()
{
    tee -a $seq_full \
    | sed \
	-e "s/for host $hostname/for host HOSTNAME/g" \
	-e "s/for host localhost/for host HOSTNAME/g" \
	-e "s/for host localhost\.localdomain/for host HOSTNAME/g" \
	-e "s/for host local:/for host HOSTNAME/g" \
	-e "s/for host \"localhost\"/for host \"HOSTNAME\"/g" \
	-e "s/for host \"localhost\.localdomain\"/for host \"HOSTNAME\"/g" \
	-e "s/for host \"local:\"/for host \"HOSTNAME\"/g" \
	-e "s/for host \"$hostname\"/for host \"HOSTNAME\"/g" \
	-e '/^Roll .*\/NOTICES/d' \
	-e '/^Start .*\/NOTICES/d' \
	-e '/^Start \[/s/ 2[0-9:. -]*/ DATESTAMP/' \
	-e '/^End \[/s/ 2[0-9:. -]*/ DATESTAMP /' \
	-e '/^Add merge callback for: pmlogger_daily_report/d' \
	-e '/^Merge callbacks .../d' \
	-e '/^callback: .*pmlogger_daily_report/d' \
	-e "s;$tmp;TMP;g" \
	-e "s/^\([+-][+-][+-] TMP\...t*\).*/\1/" \
	-e '/^-rw-r--r--/s/-rw-r--r--.* 2011/-rw-r--r-- ... 2011/' \
	-e `echo $PCP_LOG_DIR | sed -e 's/\//\\\\\//g'`'\/NOTICES/d'
}

_setup()
{
    $sudo rm -f $tmp.arch/*
    sudo pmlogcp archives/rewrite $tmp.arch/20111001.21.10
    sudo pmlogcp archives/rewrite $tmp.arch/20111001.22.10
    echo "global { time -> +1:00:00 }" >$tmp.conf.time
    $sudo $PMLOGREWRITE -c $tmp.conf.time -i $tmp.arch/20111001.22.10

    # pmlogger_daily runs as the user "pcp" ... fix ownership
    #
    find $tmp.arch/* -type f | xargs -r $sudo chown $PCP_USER:$PCP_GROUP
    find $tmp.arch/* -type d | xargs -r $sudo chown $PCP_USER:$PCP_GROUP
    $sudo touch $tmp.log
    $sudo chown $PCP_USER:$PCP_GROUP $tmp.log
}

_cmp()
{
    ok=true
    for ext in 0 index meta
    do
	if [ ! -f "$1.$ext" ]
	then
	    echo "Arrgh ... $1.$ext missing" | _filter
	    ok=false
	fi
	if [ ! -f "$2.$ext" ]
	then
	    echo "Warning: $2.$ext missing" | _filter
	    ok=false
	fi
    done
    $ok || return
    pmdumplog -z -dilmst $1 \
    | tee -a $seq_full \
    | sed -e '/^\[[0-9][0-9]* bytes]/d' >$tmp.in

    pmdumplog -z -dilmst $2 \
    | tee -a $seq_full \
    | sed -e '/^\[[0-9][0-9]* bytes]/d' >$tmp.out
    diff -u $tmp.in $tmp.out | _filter
}

# take explicit control of the umask
#
umask 022

mkdir $tmp.arch
$sudo chown $PCP_USER:$PCP_GROUP $tmp.arch

# metrics in play from archives/rewrite archive
#
# sample.bin PMID: 29.0.6
#     Data Type: 32-bit int  InDom: 29.2 0x7400002
#     Semantics: instant  Units: none
#     inst [100 or "bin-100"] value 100
#     inst [200 or "bin-200"] value 200
#     inst [300 or "bin-300"] value 300
#     inst [400 or "bin-400"] value 400
#     inst [500 or "bin-500"] value 500
#     inst [600 or "bin-600"] value 600
#     inst [700 or "bin-700"] value 700
#     inst [800 or "bin-800"] value 800
#     inst [900 or "bin-900"] value 900
# 
# sampledso.double.hundred PMID: 30.0.27
#     Data Type: double  InDom: PM_INDOM_NULL 0xffffffff
#     Semantics: instant  Units: none
#     value 100
#
# Log Label (Log Format Version 2)
# Performance metrics from host bozo-laptop
#   commencing Sat Oct  1 21:10:11.751 2011
#   ending     Sat Oct  1 21:10:12.128 2011
# Archive timezone: EST-10
#

cat <<End-of-File >$tmp.control
\$version=1.1
LOCALHOSTNAME	n   n	$tmp.arch	-c $tmp.pmlogger
End-of-File

cat <<End-of-File >$tmp.pmlogger
log mandatory on once { sample.bin }
End-of-File

# real QA test starts here
echo "+++ should just work ... +++" | tee -a $seq_full
_setup
# need to be a place the user $PCP_USER can cd to from pmlogger_daily
#
cd /var/tmp
$sudo -u $PCP_USER -g $PCP_GROUP sh -c "umask 022; $PCP_BINADM_DIR/pmlogger_daily -P -D -c $tmp.control -x never -V -l $tmp.log"
cd $here
$sudo cat $tmp.log | _filter
mkdir $tmp.ok
$sudo mv $tmp.arch/20111001.* $tmp.ok

cat <<'End-of-File' | while read conf
global { hostname -> dreaming }
metric sampledso.double.hundred { type -> float }
End-of-File
do
    echo "$conf" >$tmp.conf
    echo | tee -a $seq_full
    echo "!!! $conf !!!" | tee -a $seq_full

    echo | tee -a $seq_full
    echo "+++ expect mismatch +++" | tee -a $seq_full
    _setup
    $sudo $PMLOGREWRITE -c $tmp.conf -i $tmp.arch/20111001.22.10
    cd /var/tmp
    $sudo -u $PCP_USER -g $PCP_GROUP sh -c "umask 022; $PCP_BINADM_DIR/pmlogger_daily -P -D -c $tmp.control -x never -V -l $tmp.log"
    cd $here
    $sudo cat $tmp.log | _filter

    echo | tee -a $seq_full
    echo "+++ expect fix via local config file +++" | tee -a $seq_full
    _setup
    $sudo $PMLOGREWRITE -c $tmp.conf -i $tmp.arch/20111001.22.10
    $sudo cp $tmp.conf $tmp.arch/pmlogrewrite
    cd /var/tmp
    $sudo -u $PCP_USER -g $PCP_GROUP sh -c "umask 022; $PCP_BINADM_DIR/pmlogger_daily -P -D -c $tmp.control -x never -V -l $tmp.log"
    cd $here
    $sudo cat $tmp.log | _filter
    _cmp $tmp.ok/20111001 $tmp.arch/20111001

    echo | tee -a $seq_full
    echo "+++ expect fix via local symlink config file +++" | tee -a $seq_full
    _setup
    $sudo $PMLOGREWRITE -c $tmp.conf -i $tmp.arch/20111001.22.10
    $sudo ln -s $tmp.conf $tmp.arch/pmlogrewrite
    cd /var/tmp
    $sudo -u $PCP_USER -g $PCP_GROUP sh -c "umask 022; $PCP_BINADM_DIR/pmlogger_daily -P -D -c $tmp.control -x never -V -l $tmp.log"
    cd $here
    $sudo cat $tmp.log | _filter
    _cmp $tmp.ok/20111001 $tmp.arch/20111001

    echo | tee -a $seq_full
    echo "+++ expect fix via global config dir +++" | tee -a $seq_full
    _setup
    $sudo $PMLOGREWRITE -c $tmp.conf -i $tmp.arch/20111001.22.10
    $sudo mkdir -p $PCP_VAR_DIR/config/pmlogrewrite
    $sudo cp $tmp.conf $PCP_VAR_DIR/config/pmlogrewrite/qa.$seq
    cd /var/tmp
    $sudo -u $PCP_USER -g $PCP_GROUP sh -c "umask 022; $PCP_BINADM_DIR/pmlogger_daily -P -D -c $tmp.control -x never -V -l $tmp.log"
    cd $here
    $sudo cat $tmp.log | _filter
    _cmp $tmp.ok/20111001 $tmp.arch/20111001
    $sudo rm -f $PCP_VAR_DIR/config/pmlogrewrite/qa.$seq

    echo | tee -a $seq_full
    echo "+++ expect fix via symlink to global config dir +++" | tee -a $seq_full
    _setup
    $sudo $PMLOGREWRITE -c $tmp.conf -i $tmp.arch/20111001.22.10
    $sudo mkdir -p $PCP_VAR_DIR/config/pmlogrewrite
    $sudo cp $tmp.conf $PCP_VAR_DIR/config/pmlogrewrite/qa.$seq
    $sudo ln -s $PCP_VAR_DIR/config/pmlogrewrite $tmp.arch/pmlogrewrite
    cd /var/tmp
    $sudo -u $PCP_USER -g $PCP_GROUP sh -c "umask 022; $PCP_BINADM_DIR/pmlogger_daily -P -D -c $tmp.control -x never -V -l $tmp.log"
    cd $here
    $sudo cat $tmp.log | _filter
    _cmp $tmp.ok/20111001 $tmp.arch/20111001
    $sudo rm -f $PCP_VAR_DIR/config/pmlogrewrite/qa.$seq

done

echo | tee -a $seq_full
echo "+++ expect fail when -r used and local config file present +++" | tee -a $seq_full
_setup
$sudo $PMLOGREWRITE -c $tmp.conf -i $tmp.arch/20111001.22.10
$sudo cp $tmp.conf $tmp.arch/pmlogrewrite
cd /var/tmp
$sudo -u $PCP_USER -g $PCP_GROUP sh -c "umask 022; $PCP_BINADM_DIR/pmlogger_daily -P -D -r -c $tmp.control -x never -V -l $tmp.log"
cd $here
$sudo cat $tmp.log | _filter

# success, all done
exit
